<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="Shortcut Icon" href="../images/aecc.ico" type="image/x-icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="../StaticFiles/element-ui/element-ui.css">
  <link rel="stylesheet" href="../StaticFiles/nprogress/nprogress.min.css">
  <link rel="stylesheet" href="../styles/vue-page-common.css">
  <script src="../Scripts/vue.min.js"></script>
  <script src="../StaticFiles/element-ui/element-ui.js"></script>
  <script src="../Scripts/axios.min.js"></script>
  <script src="../StaticFiles/nprogress/nprogress.min.js"></script>
  <script src="../Scripts/dayjs.min.js"></script>
  <script src="../Scripts/crypto-js.js"></script>
  <script src="../Scripts/secret.js"></script>
  <script src="../Scripts/axios.config.js"></script>
  <script src="../Scripts/datav.min.vue.js"></script>
  <script src="../Scripts/lodash.min.js"></script>
  <title>批次调度、批次撤销统计看板</title>
  <style>
    html {
      background-image: url(../images/bg.jpg);
      color: #fff;
    }

    .el-form-item__label {
      color: #fff;
    }

    .header-title {
      font-size: 24px !important;
    }

    .el-form-item {
      margin-bottom: 0 !important;
    }

    .chart-title {
      text-align: center;
      padding-top: 10px;
      color: #fff;
    }

    .chart {
      width: 100%;
      height: 80px;
      font-size: 40px;
      font-weight: bold;
      cursor: pointer;
      text-align: center;
    }
  </style>
</head>

<body>
  <div id="app" v-cloak>
    <div style="padding:0 5px;">
      <!-- 看板标题组件 -->
      <vue2-board-header :title="'批次调度、批次撤销统计看板'"></vue2-board-header>
      <!-- 查询条件区start -->
      <el-form :inline="true" :model="queryInfo" ref="queryInfoRef" :label-position="'left'" :rules="rules"
        style="margin:10px 10px 0 10px;">
        <el-form-item label="操作时间:" prop="createdate">
          <el-date-picker v-model="queryInfo.createdate" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item prop='containername' label="流水卡号:">
          <el-autocomplete v-model.trim="queryInfo.containername" placeholder="请输入流水卡号" :trigger-on-focus="false"
            :fetch-suggestions="searchContainerAsync" :value-key="'containername'" :debounce="500" style="width:300px;">
          </el-autocomplete>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="searchData">查询</el-button>
        </el-form-item>
      </el-form>
      <!-- 查询条件区end -->

      <el-row>
        <el-col :span="24">
          <dv-border-box-10>
            <h3 class="chart-title">批次调度</h3>
            <div class="chart" :style="{ height: chartHeight +'px' }" id="moveChart"></div>
          </dv-border-box-10>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <dv-border-box-10>
            <h3 class="chart-title">批次撤销</h3>
            <div class="chart" :style="{ height: chartHeight +'px' }" id="revokeChart"></div>
          </dv-border-box-10>
        </el-col>
      </el-row>

      <!-- 明细数据 -->
      <el-dialog :title="chartDetail.title" :width="'98%'" :top="'10px'" :center="'center'"
        :visible.sync="chartDetail.dialogVisiable">
        <el-table v-loading="loading" v-loading.fullscreen.lock="loading" :element-loading-text="loadingText"
          element-loading-background="rgba(0,0,0,0.8)" :data="chartDetail.dataList" stripe border>
          <el-table-column type="index" width="50"></el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-download" size="mini" @click="exportDetail">
                导出
              </el-button>
            </template>
          </el-table-column>
          <el-table-column label="分厂" prop="factoryname" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="订单号" prop="mfgordername" show-overflow-tooltip></el-table-column>
          <el-table-column label="批次号" prop="containername" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作前工序" prop="fromworkflowstepname" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作后工序" prop="toworkflowstepname" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作人" prop="operater" width="100"></el-table-column>
          <el-table-column label="操作时间" prop="operatetime" width="180" :formatter="replaceDate"></el-table-column>
          <el-table-column label="操作类型" prop="notes" width="100"></el-table-column>
        </el-table>
        <el-pagination @current-change="handleChartDetailCurrentChange"
          :current-page="chartDetail.queryInfo.currentPage" :page-sizes="[chartDetail.queryInfo.pageSize]"
          :page-size="chartDetail.queryInfo.pageSize" layout="prev,pager,next,jumper,sizes,total"
          :total="chartDetail.total">
        </el-pagination>
      </el-dialog>
    </div>
  </div>
  <script src="../Scripts/echarts/echarts-5.4.0.min.js"></script>
  <script type="module">
    import { barOption, colorList } from '../Scripts/hooks/chartConfig.js'
    import { exportExcelData, getParams, fetchSuggestion } from '../Scripts/hooks/useCommonMethod.js'
    import { getFactory } from '../Scripts/hooks/fetchAPI.js'
    import { prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth } from '../Scripts/hooks/useDateShortCut.js'
    import { replaceDate } from '../Scripts/hooks/format.js'
    import Vue2BoardHeader from '../Scripts/components/Vue2BoardHeader.js'
    const app = new Vue({
      el: '#app',
      components: { Vue2BoardHeader },
      data: {
        replaceDate,
        loading: false,
        loadingText: '数据加载中...',
        queryInfo: { createdate: '', containername: '' },
        rules: {
          createdate: [{ required: true, message: '操作时间不能为空', trigger: 'change' }]
        },
        chartLoadingConfig: { text: '数据查询中...', effect: 'whirling', maskColor: 'transparent', textColor: '#fff' },
        chartDetail: {
          dialogVisiable: false,
          queryInfo: { currentPage: 1, pageSize: 12, operatetype: '', factoryname: '' },
          dataList: [], title: '', total: 0
        },
        pickerOptions: { shortcuts: [prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth] },
        chartHeight: (window.innerHeight - 270) / 2,
        baseAPIUrl: '../handler/board/ContainerMoveOrRevokeDashBoard.ashx'
      },
      methods: {
        // 查询批次号
        searchContainerAsync(value, callback) {
          fetchSuggestion({ actionName: 'getContainernameAsync', name: 'containername', value, callback });
        },
        searchData() {
          this.$refs['queryInfoRef'].validate(async (valid) => {
            if (valid) { this.getData(); }
          })
        },
        // 查询数据
        async getData() {
          const dom = document.getElementById("moveChart");
          const dom2 = document.getElementById("revokeChart");

          echarts.dispose(dom);
          echarts.dispose(dom2);

          let chart = echarts.init(dom);
          let chart2 = echarts.init(dom2);

          chart.showLoading(this.chartLoadingConfig);
          chart2.showLoading(this.chartLoadingConfig);

          const { data: { data, meta, total } } = await axios.post(`${this.baseAPIUrl}?action=getData`, {
            type: 'getData',
            ...this.queryInfo
          })

          chart.hideLoading();
          chart2.hideLoading();

          if (meta.status == 500) { return this.$message.error(meta.msg); }
          if (data.length === 0) { return; }

          this.createChart({ id: 'moveChart', data: data.filter(i => i.notes === 'MoveNon'), operatetype: '批次调度' });
          this.createChart({ id: 'revokeChart', data: data.filter(i => i.notes === 'MoveBack'), operatetype: '批次撤销' });
        },
        // 生成柱状图
        createChart({ data, id, operatetype }) {
          const dom = document.getElementById(id);
          echarts.dispose(dom);

          let chart = echarts.init(dom);
          let arrFactory = [], arrOperateQty = [], arrOperateRate = [];
          for (const i of data) {
            arrFactory.push(i.factoryname);
            arrOperateQty.push(i.operateqty);
            arrOperateRate.push(i.operaterate);
          }

          let option = barOption();
          option.grid = { left: '3%', right: '3%', bottom: '10%', containLabel: true };
          option.yAxis[0].axisLabel.formatter = function (data) { return data };
          option.yAxis[1].axisLabel.formatter = function (data) { return data + "%"; };

          let arrLegend = operatetype === '批次调度' ? ['批次调度数', '批次调度比'] : ['批次撤销数', '批次撤销比'];
          option.legend.data = arrLegend;
          option.xAxis.data = arrFactory;

          option.series[0] = _.merge(option.series[0], {
            name: operatetype === '批次调度' ? '批次调度数' : '批次撤销数',
            data: arrOperateQty,
            barMaxWidth: 35,
            barGap: '70%',
            label: { color: colorList.blue.color, formatter: '{c}' },
            itemStyle: { color: { colorStops: colorList.blue.colorStops } }
          });

          option.series[1] = _.merge(_.cloneDeep(option.series[0]), {
            name: operatetype === '批次调度' ? '批次调度比' : '批次撤销比',
            data: arrOperateRate,
            yAxisIndex: 1,
            label: { color: colorList.red.color, formatter: '{c}%' },
            itemStyle: { color: { colorStops: colorList.red.colorStops } }
          });

          chart.setOption(option);

          window.addEventListener('resize', () => {
            chart.resize();
          });

          chart.on('click', (params) => {
            const { name, value } = params;
            if (value == 0) { return; }
            this.chartDetail.queryInfo.currentPage = 1;
            this.chartDetail.queryInfo.operatetype = operatetype;
            this.chartDetail.queryInfo.factoryname = name;
            this.chartDetail.title = `${name}【${operatetype}】明细`;
            this.chartDetail.dialogVisiable = true;
            this.getDetails();
          })
        },
        // 查看明细
        async getDetails() {
          this.loading = true;
          this.loadingText = '数据加载中...';
          const { data: { data, meta, total } } = await axios.post(`${this.baseAPIUrl}?action=getDetails`, {
            type: 'getDetails',
            ...this.queryInfo,
            ...this.chartDetail.queryInfo
          })
          this.loading = false;
          if (meta.status === 500) { return this.$message.error(meta.msg); }
          this.chartDetail.dataList = data;
          this.chartDetail.total = total;
        },
        handleChartDetailCurrentChange(newPage) {
          this.chartDetail.queryInfo.currentPage = newPage;
          this.getDetails();
        },
        exportDetail() {
          const config = {
            url: `${this.baseAPIUrl}?action=exportExcel`,
            data: {
              type: 'exportExcel',
              ...this.queryInfo,
              ...this.chartDetail.queryInfo,
              title: this.chartDetail.title
            }
          }
          exportExcelData(config, this);
        },
      },
      async mounted() {
        const startOfThisMonth = dayjs().startOf('month').format('YYYY-MM-DD');
        const endOfThisMonth = dayjs().endOf('month').format('YYYY-MM-DD');
        this.queryInfo.createdate = [startOfThisMonth, endOfThisMonth];
        this.searchData();
        window.addEventListener('resize', () => {
          this.chartHeight = (window.innerHeight - 270) / 2
        });
      }
    })
  </script>
</body>

</html>